﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL.Classes;

public partial class CapNhatThongTinCaNhan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Session["TenDangNhap"] == "" && Session["QuyenHan"].ToString() == "")
            {
                Response.Redirect("~/DangKy.aspx");
            }
            else
            {
                PopulateControls();
            }
        }
    }
    private TaiKhoan GetData()
    {
        TaiKhoan data = null;
        if (lbID.Text != "")
            // lay thong tin cu tu Database de cap nhat
            data = TaiKhoan.LayThongTinTheoMaTaiKhoan(lbID.Text);
        else
            data = new TaiKhoan();
        // them moi bai viet
        data.HoVaTen = txtHoTen.Text;
        data.NgaySinh = txtNgaySinh.Text;
        data.GioiTinh = drlGioiTinh.Text;
        data.DiaChi = txtDiaChi.Text;
        data.SoDienThoai = txtSoDT.Text;
        data.SoCMND = txtSoCMND.Text;
        return data;
    }
    private void PopulateControls()
    {
        TaiKhoan data = TaiKhoan.LayThongTinTheoTenDangNhap(Session["TenDangNhap"].ToString());
        if (data == null)
            Response.Redirect("~/TrangCaNhan.aspx?TaiKhoanID=" + Session["MaTaiKhoan"].ToString());
        // Nguoc lai thi dua du lieu vao form de tien hanh cap nhap 
        // Dat ten trang web
        lbID.Text = data.MaTaiKhoan.ToString();
        // lbThongTinCaNhan.Text = Session["HoVaTen"].ToString() + " Cập nhật thông tin";
        txtHoTen.Text = data.HoVaTen;
        txtNgaySinh.Text = data.NgaySinh;
        drlGioiTinh.Text = data.GioiTinh;
        txtDiaChi.Text = data.DiaChi;
        txtDuongDan.Text = data.AnhDaiDien;
        txtSoDT.Text = data.SoDienThoai;
        txtSoCMND.Text = data.SoCMND;
    }
    protected void btnTaiLen_Click(object sender, EventArgs e)
    {
        HttpPostedFile file = FulTaiAnh.PostedFile;
        if (FulTaiAnh.HasFile == false || file.ContentLength > 300000)
        {
            lbTaiAnh.Text = "Vui lòng chọn file <300kB";
        }
        else
        {
            try
            {
                string strPath = Server.MapPath("~/HinhAnh/" + FulTaiAnh.FileName);
                FulTaiAnh.SaveAs(strPath);
                txtDuongDan.Text = "~/HinhAnh/" + FulTaiAnh.FileName.ToString();
                lbTaiAnh.Text = "Tải lên thành công!";
            }
            catch
            {
                lbTaiAnh.Text = "Tải lên thất bại!";
            }
        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            bool rs = false;
            // Lay du lieu tu form 
            TaiKhoan data = GetData();
            // ID > 0 ==> Cap nhat va hien thong bao 
            if (data.MaTaiKhoan > 0)
            {
                rs = TaiKhoan.Sua_ThongTinCaNhan(data);
                if (rs)
                {
                    Session["HoVaTen"] = data.HoVaTen;
                    Response.Redirect("~/TrangCaNhan.aspx?TaiKhoanID=" + Session["MaTaiKhoan"].ToString());
                }
            }
        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/TrangCaNhan.aspx?TaiKhoanID=" + Session["MaTaiKhoan"]);
    }

}
